package com.macro.mall.portal.config.sharding;

/**
 * @author wwj
 */
public class ShardingUtils {

    public static Integer getPhysicNodeByVisualNode(int mod, String virtualNodeCountRang){

        String[] rangArr = virtualNodeCountRang.split(ShardingConstant._DEFAULT_SEPARATOR_COMMA);
        Integer physicCount = null;
        for (int i = 0; i < rangArr.length; i++) {
            String[] tmpArr = rangArr[i].split("-");
            if (mod >= Integer.parseInt(tmpArr[0]) && mod <= Integer.parseInt(tmpArr[1])) {
                physicCount = i;
                break;
            }
        }
        return physicCount;
    }

    /**
     * 通过位运算获取一个数的绝对值, 位运算比较快
     *
     * @param value 正数或负数
     * @return
     */
    public static Integer getAbsoluteValue(Integer value) {
        Integer absoluteValue = 0;
        if (value != null) {
            absoluteValue = (value + (value >> 31)) ^ (value >> 31);
        }
        return absoluteValue;
    }
}
